trigger DiscussionTopicBeforeInsert on DiscussionTopic__c bulk (before insert) {
    if (!TeamUtil.currentlyExeTrigger) {
        try {           
            
            List<String> idsTeam = new List<String>();
            List<String> queueNames = new List<String>();
            for(DiscussionTopic__c t : trigger.new){
                idsTeam.add(t.DiscussionForum__c);
                queueNames.add('Discussion' + t.DiscussionForum__c);
            }
            
            // Check to see if this is an admin user.  If so they can create in any team 
            List<User> teamAdmin = [Select id, Profile.PermissionsModifyAllData, ProfileId, Name From User where id =:UserInfo.getUserId() limit 1];
            Boolean isAdmin = false;
        
            if(teamAdmin[0].Profile.PermissionsModifyAllData){
                //Is SysAdmin
                // Might need to do this per author.  What if mutiple authors come through the trigger???
                isAdmin = true;     
            }       
            
            Map<Id, DiscussionProfile__c> profileMap = new Map<Id, DiscussionProfile__c>();
            if(!isAdmin){
                // Check to see if the user is in the team and if so then use that Team Profile
                for(DiscussionMember__c tm: [select DiscussionForum__c, DiscussionProfile__r.ManageDiscussionForums__c, DiscussionProfile__r.CreateDiscussionTopics__c  from DiscussionMember__c where User__c =: UserInfo.getUserId() and DiscussionForum__c in: idsTeam]) {
                	profileMap.put(tm.DiscussionForum__c, tm.DiscussionProfile__r);
                }                                 
            
                // If the user is not a membe of the team then we have to go to to the public profile of the team
                // to figure out the permissions.
                for(DiscussionForum__c t: [select PublicProfile__c, PublicProfile__r.ManageDiscussionForums__c, PublicProfile__r.CreateDiscussionTopics__c from DiscussionForum__c where Id in: idsTeam]) {
                    if(profileMap.get(t.Id) == null) profileMap.put(t.Id, t.PublicProfile__r);                          
                }
            }               
            
            // Get the owner queue
            Map<String, Id> queueMap = new Map<String, Id>();
            for(Group g: [select id, Name from Group where name in: queueNames and type = 'Queue' limit 1]) {
                queueMap.put(g.Name, g.Id);
            }
            
            // 
            for(DiscussionTopic__c t : trigger.new){
                
                // Check to see if this user can create discussion topic
                DiscussionProfile__c profile = profileMap.get(t.DiscussionForum__c);
                
                if(!isAdmin && !profile.CreateDiscussionTopics__c && !profile.ManageDiscussionForums__c)
                    t.addError('Cannot Insert A New Discussion Topic. Insufficient Privileges');
                
                t.PostedBy__c   = UserInfo.getUserId();  
                t.PostedDate__c = System.now(); 
                
                Id queueId = queueMap.get('Discussion' + t.DiscussionForum__c);
                if(queueId != null)
                    t.OwnerId = queueId;

            }
            
        } finally {
            TeamUtil.currentlyExeTrigger = false;
        }
    }           
}